import copy
import torch
from torch.utils import data
from trainers.fedbase.FedClientBase import FedClientBase

import logging
# logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logging.basicConfig(format ='%(message)s')
logger = logging.getLogger()
logger.setLevel(logging.INFO)



class FedReptileClient(FedClientBase):
    """
    训练相关的参数本来应该是服务器发送过来的，包括模型、参数啥的。训练数据应该是本地的。
    这里为了方便，只模拟联邦学习的效果和逻辑，不模拟联邦学习的真是过程。
    1. 数据由服务器分成独立同分布，或者非独立同分布。然后分配给各个客户端。而非客户端在本地读取。
    2. 训练的模型、参数在客户端配置。
    3. 客户端完成训练过程、服务端完成聚合过程。客户端和服务端只发送和传输模型和优化器的权重参数。完成聚合和分配的通信逻辑。

    接收参数，完成训练。主要是一次训练的参数。
    每一个算法都应该提供梯度的返回值。即在客户端计算出梯度。其实在服务器计算也无可厚非。

    Returns:
        [type]: [description]
    """    

    def __init__(self,datapair,model):
        super(FedReptileClient, self).__init__(datapair,model)    
        logger.info("FedReptileClient init----")


    def set_parameters(self,client_params):
        """从服务器加载训练的参数。并根据batch_size等参数划分数据集。

        Args:
            client_params (dict): 客户端进行训练的相关参数
        """     
        # 训练基本参数(包含记载数据)---------------------------------------------------------------------------------------
        super().set_parameters(client_params)   
        # FedAmpClient策略参数-------------------------------------------------------------------------
        # fedper数据集划分参数
        if("support_ratio" in client_params):
            self.support_ratio=client_params["support_ratio"]
        else:
            self.support_ratio = 0.7
        logger.info("FedReptileClient set_parameters:support_ratio={}\t ".format(self.support_ratio))

    
    def process_data(self):
        """服务器会调用这个函数进行数据初始化。对于reptile来说，其本地训练不再区分support还是query。
        可以直接用self.train_dl进行训练。
        """      
        x,y = self.datapair
        assert len(x) == len(y)
        # 将数据转换成tensor,为了加快速度，通过共享内存的方式
        x_train_tensor = torch.FloatTensor(x).to(self.device)
        y_train_tensor = torch.from_numpy(y).to(self.device)
        # print(x_train_tensor.dtype,y_train_tensor.dtype)
        train_ds = data.TensorDataset(x_train_tensor,y_train_tensor)
        self.train_dl = data.DataLoader(train_ds,batch_size=self.batch_size,shuffle=True)

        # 每一个round包含多少个iteration
        self.iteration  = self.epochs*(len(self.train_dl))
        logger.info("FedReptileClient process_data: iteration:{}\t".format(self.iteration))
        self.process_data_support_and_query()
        return self.train_dl  

    # 注意，这里将support&&query set都设置为一个batch，相当于one batch梯度下降。
    def process_data_support_and_query(self):
        x,y = self.datapair
        assert len(x) == len(y)
        sq_point  = int(len(y)*self.support_ratio)
        # 将数据转换成tensor,为了加快速度，通过共享内存的方式
        x_support_tensor = torch.FloatTensor(x[:sq_point]).to(self.device)
        y_support_tensor = torch.from_numpy(y[:sq_point]).to(self.device)
        x_query_tensor = torch.FloatTensor(x[sq_point:]).to(self.device)
        y_query_tensor = torch.from_numpy(y[sq_point:]).to(self.device)
        # print(x_train_tensor.dtype,y_train_tensor.dtype)
        support_ds = data.TensorDataset(x_support_tensor,y_support_tensor)
        self.support_dl = data.DataLoader(support_ds,batch_size=self.batch_size,shuffle=True)
        query_ds  = data.TensorDataset(x_query_tensor,y_query_tensor)
        self.query_dl = data.DataLoader(query_ds,batch_size=len(query_ds),shuffle=True)

        self.iteration  = self.epochs*(len(self.support_dl))
        logger.info("FedReptileClient process_data_support_and_query: iteration_support_dl:{}\t".format(self.iteration))
        return self.support_dl,self.query_dl
 
    # 用来训练当前的本地模型。每次训练返回损失值running_loss和running_accuracy
    def train_reptile(self):
        self.model.train()
        loss_sum = 0.
        loss_times = 0.
        correct_sum = 0.
        total_size = 0        
        # 保留原始梯度的一个副本
        model_state_dict = copy.deepcopy(self.model.state_dict())
        for epoch in range(self.epochs):
            # 使用support_set进行梯度下降
            for batch_x,batch_y in self.train_dl:
                # 正向传播
                outputs = self.model(batch_x)
                _,predicted = torch.max(outputs,1)
                correct_sum += (predicted == batch_y).sum().item()
                total_size += len(batch_y)
                
                # 计算loss
                loss = self.loss_func(outputs,batch_y.long())
                loss_sum +=loss.item()
                loss_times+=1
                # 反向传播
                self.optimizer.zero_grad()
                loss.backward()

                # 梯度下降
                self.optimizer.step()
            
        # reptile不需要进行queryset的梯度下降。而是使用outer_lr进行放缩。这个放缩与maml一样，放到服务器上。

        # print(self.optimizer.state_dict())
        return loss_sum/loss_times,100*correct_sum/total_size

    # 用来训练当前的本地模型。每次训练返回损失值running_loss和running_accuracy
    def train_reptile_by_grad(self):
        self.model.train()
        loss_sum = 0.
        loss_times = 0.
        correct_sum = 0.
        total_size = 0        
        # 保留原始梯度的一个副本
        model_state_dict = copy.deepcopy(self.model.state_dict())
        for epoch in range(self.epochs):
            # 使用support_set进行梯度下降
            for batch_x,batch_y in self.train_dl:
                # 正向传播
                outputs = self.model(batch_x)
                _,predicted = torch.max(outputs,1)
                correct_sum += (predicted == batch_y).sum().item()
                total_size += len(batch_y)
                # 计算loss
                loss = self.loss_func(outputs,batch_y.long())
                loss_sum +=loss.item()
                loss_times+=1
                # 反向传播
                self.optimizer.zero_grad()
                loss.backward()

                # 梯度下降
                self.optimizer.step()
            
        # reptile不需要进行queryset的梯度下降。而是使用outer_lr进行放缩。这个放缩与maml一样，放到服务器上。
        self.grad_dict={}
        new_state_dict = self.model.state_dict()
        for name in model_state_dict:
            self.grad_dict[name]=new_state_dict[name]-model_state_dict[name]

        # print(self.optimizer.state_dict())
        return loss_sum/loss_times,100*correct_sum/total_size


    # 进行finetuning之后测试
    def test_reptile(self):
        self.model.train()
        # 对于reptile而言直接进行本地适应性训练。不需要冻结层。
        for epoch in range(self.epochs+10):
            # 使用support_set进行梯度下降
            for batch_x,batch_y in self.support_dl:
                # 正向传播
                outputs = self.model(batch_x)

                # 计算loss
                loss = self.loss_func(outputs,batch_y.long())
                # 反向传播
                self.optimizer.zero_grad()
                loss.backward()

                # 梯度下降
                self.optimizer.step()
        # 进入测试
        self.model.eval()
        correct = 0
        total =0
        with torch.no_grad():
            for features,labels in self.query_dl:
                outputs = self.model(features)
                # print(outputs.shape)
                _,predicted = torch.max(outputs,1)
                total += labels.size(0)
                correct += (predicted == labels).sum().item()

        accuracy = 100*correct/total
        return accuracy
